package com.totoro.service.impl;

import com.totoro.mapper.ProductMapper;
import com.totoro.pojo.*;
import com.totoro.service.EvaluationService;
import lombok.SneakyThrows;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;
import java.util.UUID;

/**
 * @author TOTORO
 * @date 2021/7/24 15:01
 */
@Service
public class EvaluationServiceImpl implements EvaluationService {

    @Autowired
    ProductMapper productMapper;

    @Override
    public Integer addProductComment(ProductComment productComment) {
        return productMapper.addProductComment(productComment);
    }


    @SneakyThrows
    @Override
    public Integer addProductCommentImage(Integer pcId, MultipartFile[] files, HttpServletRequest request) {
        System.out.println(pcId+"我的id在这里，看到了么啊");

        if (files != null && files.length > 0) {
            //循环获取file数组中得文件
            for (int i = 0; i < files.length; i++) {
                MultipartFile file = files[i];
                String uploadPath = request.getSession().getServletContext().getRealPath("/")+"static\\img\\product\\evaluation\\";
                File path =  new File(uploadPath);
                if(path == null){ path.mkdirs();}
                String filename = UUID.randomUUID().toString();
                // 通过MultipartFile的transferTo(File dest)这个方法来直接转存文件到指定的路径
                String ext =  file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
                System.err.println(ext);
                file.transferTo(new File(uploadPath+filename+file.getOriginalFilename()));
                // 打包成对象，交互dao
                CommentImage commentImage = new CommentImage();
                commentImage.setPcId(pcId);
                commentImage.setImagePath("\\static\\img\\product\\evaluation\\"+filename+file.getOriginalFilename());
                productMapper.addProductCommentImage(commentImage);
            }
        }
        return null;
    }

    @Override
    public Integer addProductAppendComment(AppendComment appendComment) {
        return productMapper.addProductAppendComment(appendComment);
    }

    @SneakyThrows
    @Override
    public Integer addProductAppendCommentImage(Integer acId, MultipartFile[] files, HttpServletRequest request) {
        if (files != null && files.length > 0) {
            //循环获取file数组中得文件
            for (int i = 0; i < files.length; i++) {
                MultipartFile file = files[i];
                String uploadPath = request.getSession().getServletContext().getRealPath("/")+"static\\img\\product\\evaluation\\";
                File path =  new File(uploadPath);
                if(path == null){ path.mkdirs();}
                String filename = UUID.randomUUID().toString();
                // 通过MultipartFile的transferTo(File dest)这个方法来直接转存文件到指定的路径
                String ext =  file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
                System.err.println(ext);
                file.transferTo(new File(uploadPath+filename+file.getOriginalFilename()));
                // 打包成对象，交互dao
                CommentImage commentImage = new CommentImage();
                commentImage.setPcId(acId);
                commentImage.setImagePath("\\static\\img\\product\\evaluation\\"+filename+file.getOriginalFilename());
                productMapper.addProductAppendCommentImage(commentImage);
            }
        }
        return null;
    }

    @Override
    public List<ProductComment> findProductAllEvaluationByPid(int pid, int type, String sort) {
        return productMapper.findProductAllEvaluationByPid(pid, type, sort);
    }

    @Override
    public List<ProductComment> findUserMultipleEvaluation(int findType, Object id, int showType, int showContent) {
        return productMapper.findUserMultipleEvaluation(findType, id, showType, showContent);
    }

    @Override
    public ProductComment findOneEvaluation(int pcId) {
        return productMapper.findOneEvaluation(pcId);
    }

    @Override
    public ProductComment findProductEvaluationByMsId(int msId) {
        return productMapper.findProductEvaluationByMsId(msId);
    }

    @Override
    public List<ProductComment> findUserAllEvaluationBySlid(SuperEvaluationFind superEvaluationFind) {
        if(null!=superEvaluationFind.getPaginationCurrent() && null!=superEvaluationFind.getPaginationPageSize()){
            superEvaluationFind.setPaginationCurrent(Integer.parseInt(superEvaluationFind.getPaginationCurrent().toString()));
            superEvaluationFind.setPaginationPageSize(Integer.parseInt(superEvaluationFind.getPaginationPageSize().toString()));
        }
        return productMapper.findUserAllEvaluationBySlid(superEvaluationFind);
    }

    @Override
    public Integer addSellerReply(SellerReply sellerReply) {
        return productMapper.addSellerReply(sellerReply);
    }

    @Override
    public Integer updateIsReply(int pcId) {
        return productMapper.updateIsReply(pcId);
    }

    @Override
    public Integer deleteEvaluationImage(int ciId) {
        return productMapper.deleteEvaluationImage(ciId);
    }

    @Override
    public Integer deleteAppendEvaImage(int aciId) {
        return productMapper.deleteAppendEvaImage(aciId);
    }

    @Override
    public Integer updateEvaluationAnonymous(int pcId) {
        return productMapper.updateEvaluationAnonymous(pcId);
    }

    @Override
    public Integer updateEvaluationIsHaveImage(int pcId, int status) {
        return productMapper.updateEvaluationIsHaveImage(pcId, status);
    }

    @Override
    public Integer updateAppendEvaIsHaveImage(int acId, int status) {
        return productMapper.updateAppendEvaIsHaveImage(acId, status);
    }
}
